home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / chrdmstr.lha / chordmaster / source / ListCreationFunctions.c < prev    next >
C/C++ Source or Header  |  1992-10-23  |  10KB  |  286 lines

  1. #include "ChordPro.h"
  2.  
  3. /********************************************************************************/
  4. /* Procedure  : create_input_list                                               */
  5. /* Purpose    : creates a linked list of match structures                       */
  6. /* Inputs     : char *inputted_notes[] - string array of the user note choice   */
  7. /*            : int nm_notes - number of notes that the user has entered        */
  8. /*            : char *main - the initial encoding of the inputted notes         */
  9. /* Outputs    : MATCH_LIST - the head of the created list                       */
  10. /* Notes      : none                                                            */
  11. /********************************************************************************/
  12.  
  13. MATCH_LIST create_input_list(char *inputted_notes[], int nm_notes, char *main)
  14. {
  15.  
  16.    MATCH_LIST head = NULL, tail;
  17.  
  18.    int counter = 0;
  19.  
  20.    head = malloc(sizeof(MATCH_ELEMENT));
  21.  
  22.    head->encoding = calloc(13, sizeof(int));
  23.    head->root = calloc(3, sizeof(int));
  24.    head->name = calloc(20, sizeof(char));
  25.    strcpy(head->encoding, main);
  26.    strcpy(head->root, inputted_notes[0]);
  27.    head->match = 0;
  28.  
  29.    tail = head;
  30.  
  31.    for (counter = 1; counter < nm_notes; counter++)
  32.    {
  33.       tail->next = malloc(sizeof(MATCH_ELEMENT));
  34.       tail = tail->next;
  35.       tail->encoding = calloc(13, sizeof(int));
  36.       tail->root = calloc(3, sizeof(int));
  37.       tail->name = calloc(20, sizeof(char));
  38.       strcpy(tail->root, inputted_notes[counter]);
  39.       strcpy(tail->encoding, main);
  40.       tail->match = 0;
  41.    }
  42.  
  43.    tail->next = NULL;
  44.    return head;
  45.  
  46. }
  47.  
  48. /********************************************************************************/
  49. /* Procedure  : root_to_index                                                   */
  50. /* Purpose    : finds the global index of a given note in the tonal array       */
  51. /* Inputs     : char *root - the note of which the index is to found            */
  52. /* Outputs    : int - the index of the note                                     */
  53. /* Notes      : uses the global array tonal_board                               */
  54. /********************************************************************************/
  55.  
  56. int root_to_index(char *root)
  57. {
  58.  
  59.    extern *tonal_board[];   
  60.  
  61.    int array_count;
  62.  
  63.    for (array_count = 0; array_count < 12; array_count++)
  64.    {
  65.       if (!(strcmp(tonal_board[array_count], root)))
  66.       return array_count;
  67.    }
  68.  
  69. }
  70.  
  71. /********************************************************************************/
  72. /* Procedure  : reorder_encoding                                                */
  73. /* Purpose    : reorders the encoding so that the root is at the front          */
  74. /* Inputs     : char *encoding - the encoding to be fixed                       */
  75. /*            : int root_index - the position of the root                       */
  76. /* Outputs    : none                                                            */
  77. /* Notes      : none                                                            */ 
  78. /********************************************************************************/
  79.  
  80. void reorder_encoding(char *encoding, int root_index)
  81. {
  82.  
  83.    int array_count;
  84.    int enco_count = 0;
  85.    char *temp_encoding = calloc(13, sizeof(char));
  86.  
  87.    strcpy(temp_encoding, encoding);
  88.  
  89.    for (array_count = root_index; array_count < 12; array_count++)
  90.    {
  91.       encoding[enco_count] = temp_encoding[array_count];
  92.       enco_count = enco_count + 1;
  93.    }
  94.  
  95.    for (array_count = 0; array_count < root_index; array_count++)
  96.    {
  97.       encoding[enco_count] = temp_encoding[array_count];
  98.       enco_count = enco_count + 1;
  99.    }
  100.  
  101.    encoding[0] = '1';
  102.  
  103. }
  104.  
  105. /***********************************************************************/
  106. /*                                                                     */
  107. /* Procedure : create_note_array                                       */
  108. /* Purpose   : finds the actual notes selected                         */
  109. /* Inputs    : char *main_enco - the binary encoding                   */
  110. /*           : char *in_notes  - the array to fill                     */
  111. /* Outputs   : none                                                    */
  112. /* Notes     : none                                                    */
  113. /***********************************************************************/
  114.  
  115. void create_note_array(char *main_enco,char *in_notes[])
  116. {
  117.  
  118.    extern *tonal_board[];
  119.  
  120.    int count = 0, current = 0;
  121.    
  122.    for (count = 0; count < 12; ++count)
  123.    {
  124.       if (main_enco[count] == '1')
  125.       {
  126.          in_notes[current] = calloc(strlen(tonal_board[count]) + 1, sizeof(char));
  127.          strcpy(in_notes[current], tonal_board[count]);
  128.          current += 1;
  129.       }
  130.    }
  131.  
  132. }
  133.  
  134. /***********************************************************************/
  135. /* Procedure : setup_search                                            */
  136. /* Purpose   : create the complete search list                         */
  137. /* Inputs    : char *main_encoding - binary encoding of input          */
  138. /*           : int nm_notes - number of notes inputted                 */
  139. /* Outputs   : MATCH_LIST - the head of the created list               */
  140. /* Notes     :                                                         */
  141. /***********************************************************************/
  142.  
  143. MATCH_LIST setup_search(char *main_encoding, int num)
  144. {
  145.  
  146.    char *in_notes[12];
  147.    MATCH_LIST head, current;
  148.    int index;
  149.  
  150.    create_note_array(main_encoding, in_notes);
  151.    head = create_input_list(in_notes, num, main_encoding);
  152.    current = head;
  153.  
  154.    while (current != NULL)
  155.    {
  156.       index = root_to_index(current->root);
  157.       current->encoding[index] = 'R';
  158.       reorder_encoding(current->encoding, index);
  159.       current = current->next;
  160.    }
  161.  
  162.    return head;
  163.  
  164. }
  165.  
  166. /***********************************************************************/
  167. /* Procedure : start_search                                            */
  168. /* Purpose   : the actual search of the chords                         */
  169. /* Inputs    : char *main_encoding - binary encoding of input          */
  170. /*           : int nm_notes - number of notes inputted                 */
  171. /* Outputs   : none                                                    */
  172. /* Notes     :                                                         */
  173. /***********************************************************************/
  174.  
  175. void start_search(char *main_encoding, int nm_notes)
  176. {
  177.  
  178.    MATCH_LIST head_of_matches = setup_search(main_encoding, nm_notes);
  179.    MATCH_LIST current_match = head_of_matches;
  180.    CHORD_LIST current_chord;
  181.    FOUND_LIST current_found;
  182.    extern CHORD_LIST head_of_chords;
  183.    extern FOUND_LIST head_of_founds;
  184.    int index = 0;
  185.    int nm_ones = 0;   
  186.    int chord_ones = 0;
  187.    head_of_founds = malloc(sizeof(FOUND_ELEMENT));
  188.    current_found = head_of_founds;
  189.  
  190.    while (current_match != NULL)
  191.    {
  192.       /* printf("Current Encoding = %s.\n", current_match->encoding); */
  193.       current_chord = head_of_chords;
  194.       while (current_chord != NULL)
  195.       {
  196.          /* printf("Current Chord = %s. \n", current_chord->encoding); */
  197.          chord_ones = 0;
  198.          for (index = 0; index < 12; index++)
  199.          {
  200.             if (current_chord->encoding[index] == '1')
  201.                   chord_ones += 1;
  202.             if (current_chord->encoding[index] == current_match->encoding[index])
  203.             {
  204.                if (current_match->encoding[index] == '1')
  205.                {
  206.                   nm_ones += 1;
  207.                }
  208.             }
  209.          }
  210.          if ((nm_ones == (chord_ones)) && (nm_ones != 0) && (nm_ones >= nm_notes))
  211.          {
  212.             current_found->next = add_new_element(current_match->root, current_chord->encoding, current_chord->name, 0); 
  213.             current_found = current_found->next;
  214.          }
  215.          else if ((nm_ones == (chord_ones - 1)) && (nm_ones != 0) && (nm_ones >= nm_notes))
  216.          {
  217.             current_found->next = add_new_element(current_match->root, current_chord->encoding, current_chord->name, 1); 
  218.             current_found = current_found->next;
  219.          }
  220.          else if ((nm_ones == (chord_ones - 2)) && (nm_ones != 0) && (nm_ones >= nm_notes))
  221.          {
  222.             current_found->next = add_new_element(current_match->root, current_chord->encoding, current_chord->name, 2);       
  223.             /* printf("Inter= %s, Notes= %s\n", current_found->chord_notes, current_found->intervals); */
  224.             current_found = current_found->next;
  225.          }
  226.          nm_ones = 0;
  227.          current_chord = current_chord->next;
  228.       }
  229.    current_match = current_match->next;
  230.    }
  231.  
  232.    current_found->next = NULL;
  233.    current_found = head_of_founds;
  234.    head_of_founds = head_of_founds->next;
  235.    free(current_found);
  236.  
  237. }
  238.  
  239. FOUND_LIST add_new_element(char *root, char *encoding, char *name, int accuracy)
  240. {
  241.    
  242.    extern *tonal_board[];
  243.    extern *inter_board[];
  244.    char *space = " \0";
  245.    int root_index, index;
  246.    FOUND_LIST head = malloc(sizeof(FOUND_ELEMENT));
  247.    
  248.    head->full_chord_name = calloc(strlen(root) + strlen(name) + 2, sizeof(char));
  249.    head->chord_notes = calloc(30, sizeof(char));
  250.    head->intervals = calloc(30, sizeof(char));
  251.   
  252.    head->match = accuracy;
  253.    
  254.    root_index = root_to_index(root);
  255.    
  256.    strcpy(head->chord_notes, tonal_board[root_index]);
  257. /*   printf("Root Inter = %s\n", inter_board[0]); */
  258.    strcpy(head->intervals, inter_board[0]); 
  259.    head->chord_notes = strcat(head->chord_notes, space);
  260.    head->intervals = strcat(head->intervals, space); 
  261.    strcpy(head->full_chord_name, root);
  262.    head->full_chord_name = strcat(head->full_chord_name, name);
  263.  
  264.    for (index = 1; index < 12; ++index)
  265.    {
  266.       if (encoding[index] == '1')
  267.       {
  268.          if ((root_index + index) > 11)
  269.          {
  270.             head->chord_notes = strcat(head->chord_notes, tonal_board[root_index + index - 12]);
  271.             head->chord_notes = strcat(head->chord_notes, space);
  272.          }
  273.          else
  274.          {
  275.             head->chord_notes = strcat(head->chord_notes, tonal_board[root_index + index]);  
  276.             head->chord_notes = strcat(head->chord_notes, space);
  277.          }
  278.          head->intervals = strcat(head->intervals, inter_board[index]);
  279.          head->intervals = strcat(head->intervals, space); 
  280.       }
  281.    }
  282.  /*  printf("Full Notes = %s. Full Name = %s Full Intervals = %s\n", head->chord_notes, name, head->intervals); */
  283.  
  284.    return head;
  285.  
  286. }